<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

	protected function _initFr() {
		$this->bootstrap('frontController');
    	$front = $this->frontController;
    	$front->setRequest(new Zend_Controller_Request_Http());

		Zend_Controller_Action_HelperBroker::addPath('KontorX/Controller/Action/Helper','KontorX_Controller_Action_Helper');

		if (null !== ($acl = Zend_Controller_Action_HelperBroker::getStaticHelper('Acl'))) {
			$acl->setForwardIfDeny('login','auth','user');
		}
	}
	
	protected function _initDbDebug() {
		if ($this->getEnvironment() == 'development') {
			$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
			$profiler->setEnabled(true);

			$this->bootstrap('db');
			$db = $this->getResource('db');
			$db->setProfiler($profiler);
		}	
	}
	
	protected function _initMyView() {
		$view = $this->getPluginResource('View')->getView();
		$view->addHelperPath('KontorX/View/Helper','KontorX_View_Helper_');
		$view->addHelperPath('Promotor/View/Helper','Promotor_View_Helper_');
	}
    
	protected function _initLog() {
		$logger = new Zend_Log();

		if ($this->getEnvironment() == 'production') {
			$mail = new Zend_Mail();
			
			$host = isset($_SERVER['SERVER_NAME'])
				?  getenv('SERVER_NAME') : getenv('HTTP_HOST');

			$mail->setFrom('no-reply@' . $host)
			     ->addTo('admin@eu1.pl');
	
			$writer = new Zend_Log_Writer_Mail($mail);
	
			// Set subject text for use; summary of number of errors is appended to the
			// subject line before sending the message.
			$writer->setSubjectPrependText('['. $host .'] Zend_Log');
	
			// Only email warning level entries and higher.
			$writer->addFilter(Zend_Log::WARN);
		} else {
			$writer = new Zend_Log_Writer_Stream(LOG_PATHNAME . 'application.log');
		}

		$logger->addWriter($writer);

		// w aplikacji wykorzystywane
		Zend_Registry::set('logger', $logger);
		Zend_Registry::set('Zend_Log', $logger);
    }
    
    protected function _initCache() {
    	$configCache = new Zend_Config_Ini(APP_PATHNAME.'/configuration/cache.ini', APP_ENV, array('allowModifications' => true));
		$configCache = new KontorX_Config_Vars($configCache);

		$cacheDatabase = Zend_Cache::factory(
		    $configCache->database->frontend->name,
		    $configCache->database->backend->name,
		    $configCache->database->frontend->options->toArray(),
		    $configCache->database->backend->options->toArray()
		);

		Zend_Db_Table_Abstract::setDefaultMetadataCache($cacheDatabase);
		Promotor_Model_Abstract::setDefaultResultCache($cacheDatabase);
		Zend_Paginator::setCache($cacheDatabase);
    }
}